import {Component, EventEmitter, Input, OnChanges, OnInit, Output} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {BaseService} from '../../../../shared/service/base.service';
import {ApiUrl} from '../../../../config/api-url';

@Component({
  selector: 'app-scheduler-edit',
  templateUrl: './scheduler-edit.component.html',
  styleUrls: ['./scheduler-edit.component.less']
})
export class SchedulerEditComponent implements OnInit, OnChanges {
  // 加载动画
  isOkLoading: boolean;
  // 模态框表单
  formModel: FormGroup;
  // 是否可读，修改时只能修改表达式，其他都可读
  inputDisabled: boolean;
  // 是否显示模态框
  @Input()
  isVisible: boolean;
  // 当前任务对象，用于修改
  @Input()
  currentJob;
  @Output()
  emitVisible: EventEmitter<any> = new EventEmitter();
  // 模态框标题
  title: any;

  constructor(private fb: FormBuilder, public baseService: BaseService) {

  }

  ngOnInit() {

  }

  ngOnChanges() {
    this.init();
  }

  init() {
    console.log("this.inputDisabled1" + this.inputDisabled)
    if (this.currentJob != null) {
      this.title = '编辑任务';
      this.inputDisabled = true;
      this.resetForm();
    } else {
      this.title = '添加任务';
      this.inputDisabled = false;
      this.buildForm();
    }
  }

  // 编辑默认当前对象的值
  resetForm() {
    this.formModel.reset({
      id: this.currentJob.id,
      jobName: this.currentJob.job_NAME,
      jobClassName: this.currentJob.job_CLASS_NAME,
      jobGroup: this.currentJob.job_GROUP,
      cronExpression: this.currentJob.cron_EXPRESSION
    });
  }

  // 新增默认为空
  buildForm() {
    this.formModel = this.fb.group({
      jobName: [null, [Validators.required]],
      jobClassName: [null, [Validators.required]],
      jobGroup: [null, [Validators.required]],
      cronExpression: [null, [Validators.required]]
    });
  }

  // 取消编辑
  handleCancel() {
    this.isOkLoading = false;
    this.isVisible = false;
    let body = {'isVisible': this.isVisible};
    this.emitVisible.emit(body);
  }

  // 确认编辑
  handleOk() {
    this.isOkLoading = true;

    // 校验非空和其他非法输入
    if (!this.baseService.zorro.validateFormValidate(this.formModel)) {
      this.isOkLoading = false;
      return;
    }

    // 获取到当前最新的对象数据
    let job = this.formModel.value;

    if (!this.inputDisabled) {
      // 新增
      this.baseService.post(ApiUrl.JobUrl, job).subscribe((res) => {
        if (res.status == 0) {
          this.baseService.layer.messageSuccess('添加成功');
          this.commonHandle();
        } else {
          this.baseService.layer.messageError('添加失败：' + res.msg);
        }
        this.isOkLoading = false;
      });
    } else {
      // 修改
      this.baseService.put(ApiUrl.JobUrl + '/reschedule', job).subscribe((res) => {
        if (res.status == 0) {
          this.baseService.layer.messageSuccess('修改成功');
          this.commonHandle();
        } else {
          this.baseService.layer.messageError('修改失败：' + res.msg);
        }
        this.isOkLoading = false;
      });
    }
  }

  // 公用，子传值给父
  commonHandle() {
    this.isOkLoading = false;
    this.isVisible = false;
    let body = {'isVisible': this.isVisible, 'addOrUpdate': true};
    this.emitVisible.emit(body);
  }
}
